package edu.uhcl.csci4838.group3.contentstorage;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.format.DateFormat;

/*
 * XXX
 */

/**
 * XXX
 * 
 * @author Paul Bradt
 */
public class CameraController {
	
	/**
	 * XXX
	 */
	private static int TAKE_PICTURE = 1;
	
	/**
	 * XXX
	 */
	private String dirName;
	
	/**
	 * XXX
	 */
	private Uri outputFileUri;
	
	/**
	 * XXX
	 * 
	 * @param semesterName
	 *            Semester folder name
	 * @param courseName
	 *            Course folder name
	 */
	public CameraController(final Context context,
			final String semesterName,
			final String courseName) {
		this.setDirName((semesterName
				+ File.separator + courseName));
	}
	
	/**
	 * Returns dirName.
	 * 
	 * @return dirName
	 * @see #setDirName
	 */
	
	public String getDirName() {
		return this.dirName;
	}
	
	/**
	 * Sets dirName.
	 * 
	 * @param dirName
	 *            This will handle the current path for picture for purpose of
	 *            write and delete
	 * @see #getDirName
	 */
	
	public void setDirName(final String dirName) {
		this.dirName = dirName;
	}
	
	/**
	 * XXX
	 * 
	 * @param fileName
	 * @return
	 */
	public boolean deletePicture(String fileName) {
		
		final File file = new File(this.dirName,
				fileName);
		
		if(file.exists() == true)
		{
			file.delete();
		}
		
		return false;
	}
	
	/**
	 * XXX
	 * 
	 * @return
	 */
	
	public boolean writePicture() {
		
		final String rightNow = DateFormat.format("HH:mm:ss", System.currentTimeMillis()).toString(); //$NON-NLS-1$
		
		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		
		final File file = new File(this.dirName,
				rightNow
						+ ".jpg"); //$NON-NLS-1$
		
		this.outputFileUri = Uri.fromFile(file);
		intent.putExtra(MediaStore.EXTRA_OUTPUT,
				this.outputFileUri);
		
		if(file.exists() && file.canRead())
		{
			return true;
		}
		
		
		return false;
	}
	
	/**
	 * XXX
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 * @return 
	 */
	protected String onActivityResult(final int requestCode) {
		String onActivityFileName = ""; //$NON-NLS-1$
		if (requestCode == CameraController.TAKE_PICTURE) {
			onActivityFileName = this.outputFileUri.toString();
		}
		return onActivityFileName;
		
	}
	
}
